<?php

namespace Gds\AchatBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Gds\AchatBundle\Entity\BonCommandeFournisseur;
use Gds\AchatBundle\Form\BonCommandeFournisseurType;
use Gds\AchatBundle\Entity\LigneBonCommandeFournisseur;
use Gds\AchatBundle\Entity\BonLivraisonFournisseur;
use Gds\AchatBundle\Form\BonLivraisonFournisseurType;
use Gds\ArticleBundle\Entity\Societe;
use Symfony\Component\HttpFoundation\Response;
use Gds\AchatBundle\Entity\StockAchat;
use Gds\AchatBundle\Entity\LigneBonLivraisonFournisseur;
class BonCommandeController extends Controller {

    public function indexAction() {
        $em = $this->getDoctrine()->getEntityManager();
        $societe = $em->getRepository('GdsArticleBundle:Societe')->findAll()[0];
        $ref = $societe->getPrefixeBonCommandeFournisseur();
        $ref_num = $societe->getNumeroBonCommandeFournisseur();
        $bon_commande = new BonCommandeFournisseur();
        $bon_commande->setReference($ref . ' ' . $ref_num);
        $request = $this->getRequest();
        $form = $this->createForm(new BonCommandeFournisseurType, $bon_commande);
        if ($request->isMethod('POST')) {
            try {
                $articles = json_decode($request->get('articles'));
                $heure = $request->get('heure');
                $form->bind($request);
                $bon_commande = $form->getData();
                $bon_commande->setHeure($heure);
                foreach ($articles->articles as $article) {
                    $ligne = new LigneBonCommandeFournisseur();
                    $art = $em->getRepository('GdsArticleBundle:Article')->find($article->id);
                    $ligne->setArticle($art);
                    $ligne->setQuantite($article->quantite);
                    $bon_commande->addLignesBonCommande($ligne);
                }
                $ref_num = $societe->getNumeroBonCommandeFournisseur();
                $societe->setNumeroBonCommandeFournisseur($ref_num + 1);
                $bon_commande->setReference($ref . ' ' . $ref_num);
                $em->persist($bon_commande);
                $em->persist($societe);
                $em->flush();
                $response = new Response(json_encode(array('success' => true)));
                $response->headers->set('Content-Type', 'application/json');
                return $response;
            } catch (Exception $e) {
                $response = new Response(json_encode(array('success' => false)));
                $response->headers->set('Content-Type', 'application/json');
                return $response;
            }
        }
        return $this->render('GdsAchatBundle:BonCommande:bon_commande.html.twig', array('form' => $form->createView()));
    }

    public function getAllArticlesBonCommandeAction() {
        $em = $this->getDoctrine()->getEntityManager();
        $articles = $em->getRepository('GdsArticleBundle:Article')->findAll();
        $result = array();
        $k = 0;
        foreach ($articles as $article) {
            if ($article->getIsSupprimer() == false) {
                $result[$k]['designation'] = $article->getDesignation();
                $result[$k]['reference'] = $article->getReference();
                $result[$k]['quantite'] = 1;
                $name = $article->getReference() . ' : ' . $article->getDesignation();
                $result[$k]['name'] = $name;
                $result[$k]['id'] = $article->getId();
                $k++;
            }
        }
        $response = new Response(json_encode($result));
        $response->headers->set('Content-Type', 'application/json');
        return $response;
    }

    public function getArticlesBonCommandeWhereFamillesAction() {
        $em = $this->getDoctrine()->getEntityManager();
        $request = $this->getRequest();
        $familles = json_decode($request->get('familles'));
        $sous_familles = $familles->sous_familles;
        if (count($sous_familles) == 0) {
            $familles = $familles->familles;
            $all_familles = $familles;
            foreach ($familles as $famille) {
                $sous_familles = $em->getRepository('GdsArticleBundle:Famille')->findBy(array('famille_mere' => $famille, 'is_supprimer' => 0));
                foreach ($sous_familles as $sous_famille)
                    array_push($all_familles, $sous_famille->getId());
            }
            $familles = $all_familles;
        }
        else
            $familles = $familles->sous_familles;
        $result = array();
        $k = 0;
        foreach ($familles as $id_famille) {
            $articles = $em->getRepository('GdsArticleBundle:Article')->findByFamille($id_famille);
            foreach ($articles as $article) {
                if ($article->getIsSupprimer() == false) {
                    $result[$k]['designation'] = $article->getDesignation();
                    $result[$k]['reference'] = $article->getReference();
                    $result[$k]['quantite'] = 1;
                    $name = $article->getReference() . ' : ' . $article->getDesignation();
                    $result[$k]['name'] = $name;
                    $result[$k]['id'] = $article->getId();
                    $k++;
                }
            }
        }
        if (count($familles) == 0) {
            $articles = $em->getRepository('GdsArticleBundle:Article')->findAll();
            foreach ($articles as $article) {
                if ($article->getIsSupprimer() == false) {
                    $result[$k]['designation'] = $article->getDesignation();
                    $result[$k]['reference'] = $article->getReference();
                    $result[$k]['quantite'] = 1;
                    $name = $article->getReference() . ' : ' . $article->getDesignation();
                    $result[$k]['name'] = $name;
                    $result[$k]['id'] = $article->getId();
                    $k++;
                }
            }
        }
        $response = new Response(json_encode($result));
        $response->headers->set('Content-Type', 'application/json');
        return $response;
    }

    public function getAllBonCommandesAction() {
        $em = $this->getDoctrine()->getEntityManager();
        $request = $this->getRequest();
        $num = $request->get('numero');
        $raison = $request->get('raison_social');
        $date1 = $request->get('date1');
        $date2 = $request->get('date2');
        $heure1 = $request->get('heure1');
        $heure2 = $request->get('heure2');
        $ref = $request->get('reference');
        $des = $request->get('designation');
        $is_cloture = $request->get('is_cloture');
        if ($is_cloture == 2)
            $is_cloture = '';

        $query = $em->getRepository('GdsAchatBundle:BonCommandeFournisseur')->getResult($num, $raison, $date1, $date2, $heure1, $heure2, $ref, $des, $is_cloture);
        $paginator = $this->get('knp_paginator');
        $pagination = $paginator->paginate(
                $query, $request->get('page', 1)/* page number */, 10/* limit per page */
        );

        if ($request->get('type')) {
            return $this->render('GdsAchatBundle:BonCommande:page_liste_bon_commande.html.twig', array('pagination' => $pagination));
        }
        if ($request->get('retour') != "")
            return $this->render('GdsAchatBundle:BonCommande:liste_bon_commande.html.twig', array('pagination' => $pagination));
        return $this->render('GdsAchatBundle:BonCommande:all_bon_commande.html.twig', array('pagination' => $pagination));
    }

    public function showAction() {
        $em = $this->getDoctrine()->getEntityManager();
        $request = $this->getRequest();
        $id = $request->get('id');
        $bon_commande = $em->getRepository('GdsAchatBundle:BonCommandeFournisseur')->find($id);
        $lignes = $em->getRepository('GdsAchatBundle:LigneBonCommandeFournisseur')->findBy(array('bon_commande' => $bon_commande));
        return $this->render('GdsAchatBundle:BonCommande:bon_commande_show.html.twig', array('bon_commande' => $bon_commande, 'lignes' => $lignes));
    }

    public function editAction() {
        $request = $this->getRequest();
        $id = $request->get('id');
        $em = $this->getDoctrine()->getEntityManager();
        $bon_commande = $em->getRepository('GdsAchatBundle:BonCommandeFournisseur')->find($id);
        $form = $this->createForm(new BonCommandeFournisseurType, $bon_commande);
        if ($request->isMethod('POST')) {
            foreach ($bon_commande->getLignesBonCommande() as $ligne) {
                $em->remove($ligne);
            }
            $em->flush();
            try {
                $articles = json_decode($request->get('articles'));
                $heure = $request->get('heure');
                $form->bind($request);
                $bon_commande = $form->getData();
                $bon_commande->setHeure($heure);
                foreach ($articles->articles as $article) {
                    $ligne = new LigneBonCommandeFournisseur();
                    $art = $em->getRepository('GdsArticleBundle:Article')->find($article->id);
                    $ligne->setArticle($art);
                    $ligne->setQuantite($article->quantite);
                    $bon_commande->addLignesBonCommande($ligne);
                }
                $em->persist($bon_commande);
                $em->flush();
                $response = new Response(json_encode(array('success' => true)));
                $response->headers->set('Content-Type', 'application/json');
                return $response;
            } catch (Exception $e) {
                $response = new Response(json_encode(array('success' => false)));
                $response->headers->set('Content-Type', 'application/json');
                return $response;
            }
        }
        $time = $bon_commande->getTime();
        $heure = $bon_commande->getHeure();
        $lignes = array();
        foreach ($bon_commande->getLignesBonCommande() as $k => $ligne) {
            $lignes[$k] = $ligne;
        }
        return $this->render('GdsAchatBundle:BonCommande:bon_commande_edit.html.twig', array('form' => $form->createView(), 'heure' => $heure, 'time' => $time, 'lignes' => $lignes, 'id' => $id));
    }

    public function duppliquerAction() {
        $request = $this->getRequest();
        $id = $request->get('id');
        $em = $this->getDoctrine()->getEntityManager();
        $old_bon_commande = $em->getRepository('GdsAchatBundle:BonCommandeFournisseur')->find($id);
        $bon_commande = clone $old_bon_commande;
        $societe = $em->getRepository('GdsArticleBundle:Societe')->findAll()[0];
        $pref = $societe->getPrefixeBonCommandeFournisseur();
        $num = $societe->getNumeroBonCommandeFournisseur();
        $bon_commande->setReference($pref . ' ' . $num);
        $form = $this->createForm(new BonCommandeFournisseurType, $bon_commande);
        if ($request->isMethod('POST')) {
            try {
                $articles = json_decode($request->get('articles'));
                $heure = $request->get('heure');
                $form->bind($request);
                $bon_commande = $form->getData();
                $bon_commande->setHeure($heure);
                foreach ($articles->articles as $article) {
                    $ligne = new LigneBonCommandeFournisseur();
                    $art = $em->getRepository('GdsArticleBundle:Article')->find($article->id);
                    $ligne->setArticle($art);
                    $ligne->setQuantite($article->quantite);
                    $bon_commande->addLignesBonCommande($ligne);
                }
                $ref_num = $societe->getNumeroBonCommandeFournisseur();
                $societe->setNumeroBonCommandeFournisseur($ref_num + 1);
                $bon_commande->setReference($pref . ' ' . $ref_num);
                $em->persist($societe);
                $em->persist($bon_commande);
                $em->flush();
                $response = new Response(json_encode(array('success' => true)));
                $response->headers->set('Content-Type', 'application/json');
                return $response;
            } catch (Exception $e) {
                $response = new Response(json_encode(array('success' => false)));
                $response->headers->set('Content-Type', 'application/json');
                return $response;
            }
        }
        $time = $bon_commande->getTime();
        $heure = $bon_commande->getHeure();
        $lignes = array();
        foreach ($bon_commande->getLignesBonCommande() as $k => $ligne) {
            $lignes[$k] = $ligne;
        }
        return $this->render('GdsAchatBundle:BonCommande:bon_commande_duppliquer.html.twig', array('form' => $form->createView(), 'heure' => $heure, 'time' => $time, 'lignes' => $lignes, 'id' => $id));
    }

    public function deleteAction() {
        $request = $this->getRequest();
        $id = $request->get('id');
        $em = $this->getDoctrine()->getEntityManager();
        $bon_commande = $em->getRepository('GdsAchatBundle:BonCommandeFournisseur')->find($id);
        $bon_commande->setIsSupprimer(true);
        $em->persist($bon_commande);
        $em->flush();
        return new Response('success');
    }

    public function livrerAction() {
        $em = $this->getDoctrine()->getEntityManager();
        $request = $this->getRequest();
        $id = $request->get('id');
        $bon_livraison = new BonLivraisonFournisseur();
        $bon_commande = $em->getRepository('GdsAchatBundle:BonCommandeFournisseur')->find($id);
        $fournisseur = $bon_commande->getFournisseur();
        $user = $this->getUser();
        $bon_livraison->setUser($user);
        $bon_livraison->setDepartement($user->getDepartement());
        $bon_livraison->setFournisseur($fournisseur);
        $bon_commande->setBonLivraison($bon_livraison);
        $form = $this->createForm(new BonLivraisonFournisseurType(), $bon_livraison);
        if ($request->isMethod('POST')) {
            try {
                $articles = json_decode($request->get('articles'));
                if (count($articles->articles) == 0) {
                    $response = new Response(json_encode(array('success' => false)));
                    $response->headers->set('Content-Type', 'application/json');
                    return $response;
                }
                $heure = $request->get('heure');
                $form->bind($request);
                $bon_livraison = $form->getData();
                $bon_livraison->setUser($user);
                $bon_livraison->setDepartement($user->getDepartement());
                
//                $bon_livraison->setFournisseur($fournisseur);
                $bon_commande->setBonLivraison($bon_livraison);
                $is_facturable = $request->get('facturable');
                $nom_dep = $request->get('departement');
                $departement = $em->getRepository('GdsArticleBundle:Departement')->findOneByNom($nom_dep);
                $bon_livraison->setHeure($heure);
                $bon_livraison->setIsFacturable($is_facturable);
                $bon_livraison->setIsSupprimer(false);
                $bon_livraison->setIsCloturer(false);
                $montant_ttc = 0;
                foreach ($articles->articles as $json_article) {
                    $article = $em->getRepository('GdsArticleBundle:Article')->find($json_article->id);
                    $stock = $em->getRepository('GdsArticleBundle:Stock')->findOneBy(array('article' => $article, 'departement' => $departement));
                    $prix_achat_ttc = $json_article->prix_achat * (1 + ($json_article->taux_tva / 100)) * (1 - ($json_article->remise / 100));
                    $montant_ttc = $montant_ttc + ($prix_achat_ttc * $json_article->quantite);
                    if ($is_facturable == 'true') {
                        $stock_achat = $em->getRepository('GdsAchatBundle:StockAchat')->findOneBy(array('stock' => $stock, 'is_noir' => 0, 'prix_achat_ttc' => $prix_achat_ttc));
                        if (count($stock_achat)==0) {
                            $stock_achat = new StockAchat();
                            $stock_achat->setIsNoir(false);
                            $stock_achat->setPrixAchatTtc($prix_achat_ttc);
                            $stock_achat->setQuantite($json_article->quantite);
                            $stock_achat->setStock($stock);
                        } else {
                            $stock_achat->setQuantite($stock_achat->getQuantite() + $json_article->quantite);
                        }
                        $stock->setQuantiteBlanc($stock->getQuantiteBlanc() + $json_article->quantite);
                    } else {
                        $stock_achat = $em->getRepository('GdsAchatBundle:StockAchat')->findOneBy(array('stock' => $stock, 'is_noir' => 1, 'prix_achat_ttc' => $prix_achat_ttc));
                        if (count($stock_achat)==0) {
                            $stock_achat = new StockAchat();
                            $stock_achat->setIsNoir(TRUE);
                            $stock_achat->setPrixAchatTtc($prix_achat_ttc);
                            $stock_achat->setQuantite($json_article->quantite);
                            $stock_achat->setStock($stock);
                        } else {
                            $stock_achat->setQuantite($stock_achat->getQuantite() + $json_article->quantite);
                        }
                        $stock->setQuantiteNoir($stock->getQuantiteNoir() + $json_article->quantite);
                    }
                    $em->persist($stock_achat);
                    $em->persist($stock);
                    $taux_tva = $json_article->taux_tva;
                    $remise = $json_article->remise;
                    $ligne = new LigneBonLivraisonFournisseur();
                    $ligne->setArticle($article);
                    $ligne->setQuantite($json_article->quantite);
                    $ligne->setQuantiteRetour(0);
                    $ligne->setPrixUnitaireHt($json_article->prix_achat);
                    $ligne->setTauxRemise($remise);
                    $ligne->setTauxTva($taux_tva);
                    $bon_livraison->addLigne($ligne);
                    $categorie_remise = $em->getRepository('GdsAchatBundle:CategorieRemiseFournisseur')->findOneBy(array('fournisseur' => $fournisseur, 'article' => $article));
                    if ($categorie_remise != null) {
                        $categorie_remise->setRemise($remise);
                        $categorie_remise->setPrixUnitaireHt($json_article->prix_achat);
                    } else {
                        $categorie_remise = new CategorieRemiseFournisseur();
                        $categorie_remise->setArticle($article);
                        $categorie_remise->setFournisseur($fournisseur);
                        $categorie_remise->setRemise($remise);
                        $categorie_remise->setPrixUnitaireHt($json_article->prix_achat);
                        $em->persist($categorie_remise);
                    }
                }
                $bon_livraison->setDepartement($departement);
                $bon_livraison->setReste($montant_ttc);
                $bon_livraison->setMontantTtc($montant_ttc);
                $bon_livraison->setUser($user);
                $em->persist($bon_livraison);
                $em->persist($bon_commande);
                $em->flush();
                $response = new Response(json_encode(array('success' => true)));
                $response->headers->set('Content-Type', 'application/json');
                return $response;
            } catch (Exception $e) {
                $response = new Response(json_encode(array('success' => false)));
                $response->headers->set('Content-Type', 'application/json');
                return $response;
            }
        }

        $lignes = array();
        foreach ($bon_commande->getLignesBonCommande() as $k => $ligne) {
            $l = array();
            $article = $ligne->getArticle();
            $lignes[$k]['designation'] = $article->getDesignation();
            $lignes[$k]['reference'] = $article->getReference();
            $lignes[$k]['taux_tva'] = $article->getTauxTva();
            $lignes[$k]['quantite'] = $ligne->getQuantite();
            $categorie_remise = $em->getRepository('GdsAchatBundle:CategorieRemiseFournisseur')->findOneBy(array('fournisseur' => $fournisseur, 'article' => $article));
            if ($categorie_remise != null) {
                $lignes[$k]['remise'] = $categorie_remise->getRemise();
                $lignes[$k]['prix_achat'] = $categorie_remise->getPrixUnitaireHt();
            } else {
                $lignes[$k]['remise'] = 0;
                $lignes[$k]['prix_achat'] = $article->getPrixAchatHt();
            }
            $name = $article->getReference() . ' : ' . $article->getDesignation();
            $lignes[$k]['name'] = $name;
            $lignes[$k]['id'] = $article->getId();
        }
        return $this->render('GdsAchatBundle:BonCommande:bon_commande_to_bon_livraison.html.twig', array('form' => $form->createView(), 'lignes' => $lignes, 'id' => $id, 'boncommande' => $bon_commande));
    }

    public function livrersAction() {
        $em = $this->getDoctrine()->getEntityManager();
        $request = $this->getRequest();
        $ids = $request->get('ids');
        $ids=  json_decode($ids);
        $bons_commande = $em->getRepository('GdsAchatBundle:BonCommandeFournisseur')->findByIdIn($ids);
        $bon_livraison = new BonLivraisonFournisseur();
        $fournisseur = $bons_commande[0]->getFournisseur();
        $user = $this->getUser();
        $bon_livraison->setUser($user);
        $bon_livraison->setDepartement($user->getDepartement());
        $bon_livraison->setFournisseur($fournisseur);
        $form = $this->createForm(new BonLivraisonFournisseurType(), $bon_livraison);
        if ($request->isMethod('POST')) {
            try {
                $articles = json_decode($request->get('articles'));
                if (count($articles->articles) == 0) {
                    $response = new Response(json_encode(array('success' => false)));
                    $response->headers->set('Content-Type', 'application/json');
                    return $response;
                }
                $heure = $request->get('heure');
                $form->bind($request);
                $bon_livraison = $form->getData();
                $bon_livraison->setUser($user);
                $bon_livraison->setDepartement($user->getDepartement());
                foreach ($bons_commande as $bon_commande) {
                $bon_commande->setBonLivraison($bon_livraison);
                $em->persist($bon_commande);
                }
                $is_facturable = $request->get('facturable');
                $departement =$user->getDepartement();
                $bon_livraison->setHeure($heure);
                $bon_livraison->setIsFacturable($is_facturable);
                $bon_livraison->setIsSupprimer(false);
                $bon_livraison->setIsCloturer(false);
                $montant_ttc = 0;
                foreach ($articles->articles as $json_article) {
                    $article = $em->getRepository('GdsArticleBundle:Article')->find($json_article->id);
                    $stock = $em->getRepository('GdsArticleBundle:Stock')->findOneBy(array('article' => $article, 'departement' => $departement));
                    $prix_achat_ttc = $json_article->prix_achat * (1 + ($json_article->taux_tva / 100)) * (1 - ($json_article->remise / 100));
                    $montant_ttc = $montant_ttc + ($prix_achat_ttc * $json_article->quantite);
                    if ($is_facturable == 'true') {
                        $stock_achat = $em->getRepository('GdsAchatBundle:StockAchat')->findOneBy(array('stock' => $stock, 'is_noir' => 0, 'prix_achat_ttc' => $prix_achat_ttc));
                        if (count($stock_achat)==0) {
                            $stock_achat = new StockAchat();
                            $stock_achat->setIsNoir(false);
                            $stock_achat->setPrixAchatTtc($prix_achat_ttc);
                            $stock_achat->setQuantite($json_article->quantite);
                            $stock_achat->setStock($stock);
                        } else {
                            $stock_achat->setQuantite($stock_achat->getQuantite() + $json_article->quantite);
                        }
                        $stock->setQuantiteBlanc($stock->getQuantiteBlanc() + $json_article->quantite);
                    } else {
                        $stock_achat = $em->getRepository('GdsAchatBundle:StockAchat')->findOneBy(array('stock' => $stock, 'is_noir' => 1, 'prix_achat_ttc' => $prix_achat_ttc));
                        if (count($stock_achat)==0) {
                            $stock_achat = new StockAchat();
                            $stock_achat->setIsNoir(TRUE);
                            $stock_achat->setPrixAchatTtc($prix_achat_ttc);
                            $stock_achat->setQuantite($json_article->quantite);
                            $stock_achat->setStock($stock);
                        } else {
                            $stock_achat->setQuantite($stock_achat->getQuantite() + $json_article->quantite);
                        }
                        $stock->setQuantiteNoir($stock->getQuantiteNoir() + $json_article->quantite);
                    }
                    $em->persist($stock_achat);
                    $em->persist($stock);
                    $taux_tva = $json_article->taux_tva;
                    $remise = $json_article->remise;
                    $ligne = new LigneBonLivraisonFournisseur();
                    $ligne->setArticle($article);
                    $ligne->setQuantite($json_article->quantite);
                    $ligne->setQuantiteRetour(0);
                    $ligne->setPrixUnitaireHt($json_article->prix_achat);
                    $ligne->setTauxRemise($remise);
                    $ligne->setTauxTva($taux_tva);
                    $bon_livraison->addLigne($ligne);
                    $categorie_remise = $em->getRepository('GdsAchatBundle:CategorieRemiseFournisseur')->findOneBy(array('fournisseur' => $fournisseur, 'article' => $article));
                    if ($categorie_remise != null) {
                        $categorie_remise->setRemise($remise);
                        $categorie_remise->setPrixUnitaireHt($json_article->prix_achat);
                    } else {
                        $categorie_remise = new CategorieRemiseFournisseur();
                        $categorie_remise->setArticle($article);
                        $categorie_remise->setFournisseur($fournisseur);
                        $categorie_remise->setRemise($remise);
                        $categorie_remise->setPrixUnitaireHt($json_article->prix_achat);
                        $em->persist($categorie_remise);
                    }
                }
                $bon_livraison->setDepartement($departement);
                $bon_livraison->setReste($montant_ttc);
                $bon_livraison->setMontantTtc($montant_ttc);
                $bon_livraison->setUser($user);
                $em->persist($bon_livraison);
                $em->flush();
                $response = new Response(json_encode(array('success' => true)));
                $response->headers->set('Content-Type', 'application/json');
                return $response;
            } catch (Exception $e) {
                $response = new Response(json_encode(array('success' => false)));
                $response->headers->set('Content-Type', 'application/json');
                return $response;
            }
        }

        $lignes = array();
        $k=0;
        foreach ($bons_commande as $bon_commande){
            
        foreach ($bon_commande->getLignesBonCommande() as  $ligne) {
            $l = array();
            $article = $ligne->getArticle();
            $lignes[$k]['bon_commande'] = $bon_commande->getReference();
            $lignes[$k]['bon_commande_id'] = $bon_commande->getId();
            $lignes[$k]['designation'] = $article->getDesignation();
            $lignes[$k]['reference'] = $article->getReference();
            $lignes[$k]['taux_tva'] = $article->getTauxTva();
            $lignes[$k]['quantite'] = $ligne->getQuantite();
            $categorie_remise = $em->getRepository('GdsAchatBundle:CategorieRemiseFournisseur')->findOneBy(array('fournisseur' => $fournisseur, 'article' => $article));
            if ($categorie_remise != null) {
                $lignes[$k]['remise'] = $categorie_remise->getRemise();
                $lignes[$k]['prix_achat'] = $categorie_remise->getPrixUnitaireHt();
            } else {
                $lignes[$k]['remise'] = 0;
                $lignes[$k]['prix_achat'] = $article->getPrixAchatHt();
            }
            $name = $article->getReference() . ' : ' . $article->getDesignation();
            $lignes[$k]['name'] = $name;
            $lignes[$k]['id'] = $article->getId();
            $k++;
        }
        }
        return $this->render('GdsAchatBundle:BonCommande:bons_commande_to_bon_livraison.html.twig', array('form' => $form->createView(), 'lignes' => $lignes, 'bon_livraison' => $bon_livraison));;
    }

}
